package com.zjcode.cdcer.controller;

import com.alibaba.fastjson.JSONObject;
import com.chinatechstar.component.commons.controller.BaseController;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.supports.PageData;
import com.zjcode.cdcer.config.MConsts.Common;
import com.zjcode.cdcer.model.CerArticleInfo;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.service.CerArticleInfoService;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName:ArticleInfoController
 * @功能：
 * @author：wlei
 * @日期：2022/9/3-18:03
 **/
@RestController
@Slf4j
@RequestMapping("/v1/articleinfo/")
public class ArticleInfoController extends BaseController {

    @Autowired
    private CerArticleInfoService cerArticleInfoService;

    /**
     * 新建文章数据
     *
     * @param pageData
     * @return
     */
    @PostMapping("createArticleInfo")
    public JsonObj createArticleInfo(@RequestBody PageData pageData) {
        String userid = getLoginUserId(pageData);
        String data = (String) pageData.get(Common.Data);
        CerArticleInfo record = JSONObject.parseObject(data, CerArticleInfo.class);
        record.setId(get32UUID());
        record.setCreateBy(userid);
        record.setPublishTime(new Date(System.currentTimeMillis()));
        record.setVersion(1);
        record.setInsertDatetime(new Date(System.currentTimeMillis()));
        record.setUpdateDatetime(new Date(System.currentTimeMillis()));
        int insert = cerArticleInfoService.insert(record);
        if (insert > 0) {
            return JsonObj.success("创建成功");
        } else {
            return JsonObj.error("创建失败");
        }
    }

    @PostMapping("updateArticleInfo")
    public JsonObj updateArticleInfo(@RequestBody PageData pageData) {
        String userid = getLoginUserId(pageData);
        String data = (String) pageData.get(Common.Data);

        CerArticleInfo record = JSONObject.parseObject(data, CerArticleInfo.class);
        record.setCreateBy(userid);
        record.setUpdateDatetime(new Date(System.currentTimeMillis()));
        int count = cerArticleInfoService.update(record);
        if (count > 0) {
            return JsonObj.success("更新成功");
        } else {
            return JsonObj.error("更新失败");
        }
    }

    @PostMapping("listByPage")
    public ListResult<Object> listByPage(@RequestBody PageData pageData) {
        return ResultBuilder.buildListSuccess(cerArticleInfoService.listByPage(pageData));
    }

    @PostMapping("deleteById")
    public JsonObj deleteById(@RequestBody PageData pageData) {
        String id = (String) pageData.get("id");
        int i = cerArticleInfoService.deleteById(id);
        if (i > 0) {
            return JsonObj.success("删除成功");
        } else {
            return JsonObj.error("删除失败");
        }
    }

    /**
     * 获取最新的文章
     *
     * @param pageData
     * @return
     */
    @PostMapping("listLatestArticles")
    public JsonObj listLatestArticles(@RequestBody PageData pageData) {
        // 最新信息
        List<CerArticleInfo> data4zx = cerArticleInfoService.selectAllByGroupId("1");
        // 工作动态
        List<CerArticleInfo> data4gzdt = cerArticleInfoService.selectAllByGroupId("2");
        // 公告通知
        List<CerArticleInfo> data4ggtz = cerArticleInfoService.selectAllByGroupId("3");
        // 学术天地
        List<CerArticleInfo> data4xstd = cerArticleInfoService.selectAllByGroupId("4");

        Map<String, Object> result = new HashMap<>();

        result.put("data4zx", data4zx);
        result.put("data4gzdt", data4gzdt);
        result.put("data4ggtz", data4ggtz);
        result.put("data4xstd", data4xstd);

        return JsonObj.success(result);
    }
}
